package com.lbj.dao.impl;

import com.lbj.dao.UserDao;
import com.lbj.domain.User;
import com.lbj.utils.DynamicQuerySql;
import com.lbj.utils.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class UserDaoImpl implements UserDao {
    JDBCUtil jdbcUtil = new JDBCUtil();
    Connection conn = jdbcUtil.createCon();
    @Override
    public User login(String loginAct, String loginPwd) {
        User user = null;
        String sql = "select * from tbl_user where loginAct=? and loginPwd=?";
        ResultSet rs = null;
        try {
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString( 1, loginAct);
            pst.setString(2, loginPwd);
            rs = pst.executeQuery();
            while (rs.next()) {
                String id = rs.getString(1);
                String name = rs.getString(3);
                String email = rs.getString(5);
                String lockState = rs.getString(6);
                String createTime = rs.getString(7);
                String profilePhoto = rs.getString(8);
                user = new User(id, loginAct,name,loginPwd , email, lockState,createTime,profilePhoto);

            }
            rs.close();
            pst.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return user;

    }

    @Override
    public List<User> getUsers() {
        List<User> users = new ArrayList<>();
        String sql = "select * from tbl_user ";
        ResultSet rs = null;
        try {
            PreparedStatement pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            while (rs.next()) {
                String id = rs.getString(1);
                String loginAct = rs.getString(2);
                String name = rs.getString(3);
                String loginPwd = rs.getString(4);
                String email = rs.getString(5);
                String lockState = rs.getString(6);
                String createTime = rs.getString(7);
                String profilePhoto = rs.getString(8);
                User user = new User(id, loginAct,name,loginPwd , email, lockState,createTime,profilePhoto);
                users.add(user);
            }
            rs.close();
            pst.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return users;
    }

    @Override
    public int register(User user) {
        String sql = "insert into tbl_user values(?,?,?,?,?,?,?,?) ";
        int row = 0;
        try {
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1, user.getId());
            pst.setString(2, user.getLoginAct());
            pst.setString(3, user.getName());
            pst.setString(4, user.getLoginPwd());
            pst.setString(5, user.getEmail());
            pst.setString(6, user.getLockState());
            pst.setString(7, user.getCreateTime());
            pst.setString(8, user.getProfilePhoto());
            row = pst.executeUpdate();

            pst.close();
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }

        return row;
    }

    @Override
    public int deleteUserById(String id) {
        int row = 0;
        String sql = "delete from tbl_user where id = ?";
        try {
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1,id);
            row = pst.executeUpdate();
            pst.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return row;
    }

    @Override
    public User getUserById(String id) {
        List<User> users = new ArrayList<>();
        String sql = "select * from tbl_user where id = ? ";
        ResultSet rs = null;
        User user = null;
        try {
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1,id);
            rs = pst.executeQuery();
            while (rs.next()) {
                String loginAct = rs.getString(2);
                String name = rs.getString(3);
                String loginPwd = rs.getString(4);
                String email = rs.getString(5);
                String lockState = rs.getString(6);
                String createTime = rs.getString(7);
                String profilePhoto = rs.getString(8);
                user = new User(id, loginAct,name,loginPwd , email, lockState,createTime,profilePhoto);
            }
            rs.close();
            pst.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return user;
    }

    @Override
    public int modUserById(User user) {
        int row = 0;
        String sql = "update tbl_user set loginAct=?,name=?,loginPwd=?,email=? where id =?";
        try {
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1,user.getLoginAct());
            pst.setString(2,user.getName());
            pst.setString(3,user.getLoginPwd());
            pst.setString(4,user.getEmail());
            pst.setString(5,user.getId());
            row = pst.executeUpdate();

            pst.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return row;
    }

    @Override
    public List<User> getUserByCondition(Map<String, String> conditions) {
       List<User> users = new ArrayList<>();
       String sql = DynamicQuerySql.getSql(conditions,"tbl_user");
       List<String> value = DynamicQuerySql.getCondition(conditions);
        try {
            PreparedStatement pst = conn.prepareStatement(sql);
            for(int i = 1;i<=conditions.size();i++) {
                pst.setString(i, "%"+value.get(i - 1)+"%");
            }
            ResultSet rs = pst.executeQuery();

            while (rs.next()) {
                String id = rs.getString(1);
                String loginAct = rs.getString(2);
                String name = rs.getString(3);
                String loginPwd = rs.getString(4);
                String email = rs.getString(5);
                String lockState = rs.getString(6);
                String createTime = rs.getString(7);
                String profilePhoto = rs.getString(8);
                User user = new User(id, loginAct,name,loginPwd , email, lockState,createTime,profilePhoto);
                users.add(user);
            }
            rs.close();
            pst.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return users;
    }
}